package com.car.advertising.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.car.advertising.R;
import com.car.advertising.activity.fragment.HomePageFragment;
import com.car.advertising.activity.fragment.MePageFragment;
import com.car.advertising.activity.fragment.ProjectPageFragment;
import com.car.advertising.view.NavigationMenu;
import com.support.widget.layout.BaseCommActivity;

/**
 * 首页Activity
 */
public class MainHomeActivity extends BaseCommActivity {
    //hp
    private RadioGroup menuRg;
    private RadioButton homeRbtn, projectRbtn, meRbtn;
    private HomePageFragment homePageFragment;
    private ProjectPageFragment projectPageFragment;
    private MePageFragment mePageFragmentt;
    public NavigationMenu navView;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_main_home);
        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        navView = (NavigationMenu) findViewById(R.id.nav_view);
        navView.attacthDrawer(drawerLayout);
        menuRg = (RadioGroup) findViewById(R.id.home_menu_rg);
        homeRbtn = (RadioButton) findViewById(R.id.home_page_rbtn);
        projectRbtn = (RadioButton) findViewById(R.id.project_page_rbtn);
        meRbtn = (RadioButton) findViewById(R.id.me_page_rbtn);
        menuRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.home_page_rbtn:
                        showFragment(1);
                        break;

                    case R.id.project_page_rbtn:
                        showFragment(2);
                        break;
                    case R.id.me_page_rbtn:
                        showFragment(3);
                        break;

                    default:
                        break;
                }
            }
        });
        showFragment(1);

    }

    public void showFragment(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        hideFragment(ft);
        switch (index) {
            case 1:
                if (homePageFragment != null) {
                    ft.show(homePageFragment);
                } else {
                    homePageFragment = new HomePageFragment();
                    ft.add(R.id.home_content_fl, homePageFragment);
                }
                homeRbtn.performClick();
                break;
            case 2:
                if (projectPageFragment != null) {
                    ft.show(projectPageFragment);
                } else {
                    projectPageFragment = new ProjectPageFragment();
                    ft.add(R.id.home_content_fl, projectPageFragment);
                }
                projectRbtn.performClick();
                break;
            case 3:
                if (mePageFragmentt != null) {
                    ft.show(mePageFragmentt);
                } else {
                    mePageFragmentt = new MePageFragment();
                    ft.add(R.id.home_content_fl, mePageFragmentt);
                }
                meRbtn.performClick();
                break;
        }
        ft.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (homePageFragment != null) {
            ft.hide(homePageFragment);
        }
        if (projectPageFragment != null) {
            ft.hide(projectPageFragment);
        }
        if (mePageFragmentt != null) {
            ft.hide(mePageFragmentt);
        }
    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

}
